using System;
using GameCore;
using UnityEngine;
using Object = UnityEngine.Object;

public static class CommonExtensions
{
    /// <summary>
    /// 注册的事件在self物体被销毁或关闭时自动移除, 简化于(Global.Event.AutoEvent)
    /// </summary>
    /// <param name="self"></param>
    /// <param name="eventType"></param>
    /// <param name="eventDelegate"></param>
    /// <param name="isCall">注册事件完成后调用一次?</param>
    /// <param name="args">调用一次时的参数</param>
    public static void AutoEvent(this MonoBehaviour self, Enum eventType, EventDelegate eventDelegate, bool isCall = false, params object[] args)
        => AutoEvent(self, UnEventMode.OnDestroy, eventType, eventDelegate, isCall, args);

    /// <summary>
    /// 注册的事件在self物体被销毁或关闭时自动移除, 简化于(Global.Event.AutoEvent)
    /// </summary>
    /// <param name="self"></param>
    /// <param name="eventType"></param>
    /// <param name="eventDelegate"></param>
    /// <param name="isCall">注册事件完成后调用一次?</param>
    /// <param name="args">调用一次时的参数</param>
    public static void AutoEvent(this MonoBehaviour self, UnEventMode eventMode, Enum eventType, EventDelegate eventDelegate, bool isCall, params object[] args)
        => Global.Event.AutoEvent(self, eventMode, eventType, eventDelegate, isCall, args);

    /// <summary>
    /// 派发事件, 简化于(Global.Event.Dispatch)
    /// </summary>
    /// <param name="eventName"></param>
    /// <param name="pars"></param>
    public static void Dispatch(this object self, string eventName, params object[] pars) => Global.Event.Dispatch(eventName, pars);

    /// <summary>
    /// 派发事件, 简化于(Global.Event.Dispatch)
    /// </summary>
    /// <param name="eventType"></param>
    /// <param name="pars"></param>
    public static void Dispatch(this object self, Enum eventType, params object[] pars) => Global.Event.Dispatch(eventType, pars);

    /// <summary>
    /// 加载资源, 简化于(Global.Resources.LoadAsset)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="self"></param>
    /// <param name="assetPath"></param>
    /// <returns></returns>
    public static T LoadAsset<T>(this Object self, string assetPath) where T : Object => Global.Resources.LoadAsset<T>(assetPath);

    /// <summary>
    /// 对象池产卵, 简化于(Global.Pool.Spawn)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="self"></param>
    /// <param name="assetPath"></param>
    /// <param name="parent"></param>
    /// <returns></returns>
    public static T Spawn<T>(this Object self, string assetPath, Transform parent = null) where T : Object => Global.Pool.Spawn<T>(assetPath, parent);

    /// <summary>
    /// 对象池产卵, 简化于(Global.Pool.Spawn)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="self"></param>
    /// <param name="assetObject"></param>
    /// <param name="parent"></param>
    /// <returns></returns>
    public static T Spawn<T>(this Object self, T assetObject, Transform parent = null) where T : Object => Global.Pool.Spawn(assetObject, parent);

    /// <summary>
    /// 回收对象, 简化于(Global.Pool.UnSpawn)
    /// </summary>
    /// <param name="self"></param>
    public static void UnSpawn(this Object self) => Global.Pool.UnSpawn(self);

    /// <summary>
    /// 获取对象池物体并且添加到缓存列表, 简化于(Global.Pool.SpawnList)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="command"></param>
    /// <param name="component"></param>
    /// <param name="parent"></param>
    /// <returns></returns>
    public static T SpawnList<T>(this Object self, Enum command, T component, Transform parent = null) where T : Component => Global.Pool.SpawnList(command, component, parent);

    /// <summary>
    /// 回收对象池缓存列表, 简化于(Global.Pool.UnSpawnList)
    /// </summary>
    /// <param name="command"></param>
    public static PoolListCache<T> UnSpawnList<T>(this Object self, Enum command) where T : Component => Global.Pool.UnSpawnList<T>(command);

    /// <summary>
    /// 回收对象列表, 简化于(Global.Pool.UnSpawnList)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="self"></param>
    /// <param name="command"></param>
    /// <param name="component"></param>
    /// <param name="parent"></param>
    /// <returns></returns>
    public static PoolListCache<T> UnSpawnList<T>(this Object self, Enum command, T component, Transform parent = null) where T : Component => Global.Pool.UnSpawnList(command, component, parent);

    /// <summary>
    /// 获取对象池列表, 简化于(Global.Pool.GetSpawnList)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="self"></param>
    /// <param name="command"></param>
    /// <returns></returns>
    public static PoolListCache<T> GetSpawnList<T>(this Object self, Enum command) where T : Component => Global.Pool.GetSpawnList<T>(command);

    /// <summary>
    /// 获取对象池列表索引, 简化于(Global.Pool.GetSpwanIndex)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="self"></param>
    /// <param name="command"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static T GetSpawnIndex<T>(this Object self, Enum command, int index) where T : Component => Global.Pool.GetSpawnIndex<T>(command, index);


    public static void Show(this Component self)
    {
        self.gameObject.SetActive(true);
    }

    public static void Hide(this Component self)
    {
        self.gameObject.SetActive(false);
    }
}